from rest_framework.generics import CreateAPIView
from rest_framework.permissions import IsAdminUser
from rest_framework.generics import ListCreateAPIView
from meiduo_admin.serializers.users import AdminAuthSerializer, UserSerializer
from users.models import User


class AdminAuthorizeView(CreateAPIView):
    queryset = User.objects.all()
    serializer_class = AdminAuthSerializer


# GET /meiduo_admin/users/?page=<页码>&pagesize=<页容量>&keyword=<搜索内容>
class UserInfoView(ListCreateAPIView):
    permission_classes = [IsAdminUser]
    serializer_class = UserSerializer

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            users = User.objects.filter(is_staff=False, username__contains=keyword)
        else:
            users = User.objects.filter(is_staff=False)
        return users
